package com.demo.encryptionDecryption.rsa;

import lombok.extern.slf4j.Slf4j;

/**
 * @author qingqing.liang
 * @create 2017-12-18
 **/
@Slf4j
public class Client {

    //没有进行pkcs8编码的秘钥会报错

    static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCb8xtuC9l2MtCQu3e4lf9gwR1l\n" +
            "Mz9vaNfbuYpJVdMffG9tcIi3j13N37NQHcXacu6LpYDzlPpNaVLSfKLyAI6ZrG/w\n" +
            "M6cAp88FRN7s5ayh1UV2wVROf0F7UmrJoHh73vHc2VndC/H4RzH31Ck1AzT1VwNK\n" +
            "vNoOQA7W6jOz4fWrRQIDAQAB";

    static String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJvzG24L2XYy0JC7\n" +
            "d7iV/2DBHWUzP29o19u5iklV0x98b21wiLePXc3fs1Adxdpy7oulgPOU+k1pUtJ8\n" +
            "ovIAjpmsb/AzpwCnzwVE3uzlrKHVRXbBVE5/QXtSasmgeHve8dzZWd0L8fhHMffU\n" +
            "KTUDNPVXA0q82g5ADtbqM7Ph9atFAgMBAAECgYBo5pro52ecLopFERVzQNLMDprr\n" +
            "XBGrq2aoEqKGugiOAXIit7P/ZZy/w14m4iAY1X7IyMd5EhlcwSBJHjWte9pvT8u+\n" +
            "Jdyb+F5hlgMi00/suvbT3d1KMwU9/S8fP3jFtritwEk1pGv4slQcr3P2uF9bw2pS\n" +
            "+LuHnSYw4G698i0jgQJBAMro+OtFreJC324F8TDV1RjsnBM7qQVzt8eUkmvFsrEU\n" +
            "4b6dYlWRIC/D6Dz+grthPghMS5MnWzAYWjr7Qm4V2ukCQQDEwLNOosUwYmUiYdEX\n" +
            "YZDcAotZ4v0LhMY+92gQNNugHBbzHVqOC2FhkfpK53vsmNrC2DkBzOYpLap5wD7m\n" +
            "/9v9AkBKGWwVlxuZ/ivT5Q2gdOZeNMIGcAoYeJrLoqEaBFduPZ722ki30Mi9SMC9\n" +
            "zX/2Z9uccihDM+MkHDCPS4SF6StJAkEAsUWOSMOPN1DwHlJTueVGR3RjadGCfOZc\n" +
            "WE9Bi5iUaWXlp5ZDCMhoLSkBgouetEXklLmMY3rhxShH+PqlsivFYQJBAJreug5t\n" +
            "WcC9zi9JYFSWgBMEmd4pSsYzpIUMm+gGpt8GKfWHzKUnPxaJhGbYIqodn/TpnX8t\n" +
            "rlwSkHiMCjmsv1Q=";

    public static void main(String[] args) throws Exception{

        test();

        log.info("====================分割线====================");

        test2();

        log.info("====================分割线====================");

        test3();



    }

    /**
     * 公钥加密，私钥解密
     */
    public static void test(){

        String data = "test";

        log.info("待加密数据:{}",data);

        String result = RsaUtil.encryptByPublicKey(data,publicKey);

        log.info("公钥加密结果:{}",result);

        String decryptData = RsaUtil.decryptByPrivateKey(result,privateKey);

        log.info("私钥解密结果:{}",decryptData);

    }
    /**
     * 私钥加密，公钥解密
     */
    public static void test2(){
        String data = "test";

        log.info("待加密数据:{}",data);

        String result = RsaUtil.encryptByPrivateKey(data,privateKey );

        log.info("私钥加密结果:{}",result);

        String decryptData = RsaUtil.decryptByPublicKey(result,publicKey);

        log.info("公钥解密结果:{}",decryptData);
    }

    /**
     * 用私钥签名，用公钥验证签名
     */
    public static void test3() throws Exception{
        String data = "test";

        log.info("待签名数据:{}",data);

        String sign = RsaUtil.sign(data,privateKey);

        log.info("签名:{}",sign);

        Boolean verifyResult = RsaUtil.verify(data,publicKey,sign);

        log.info("源数据，验证签名结果:{}",verifyResult);

        String fakeData = "fakeData";
        Boolean fakeResult = RsaUtil.verify(fakeData,publicKey,sign);
        log.info("被篡改数据，验证签名结果:{}",fakeResult);

    }

}
